home *** CD-ROM | disk | FTP | other *** search
/ American Osteopathic Ass…tion Yearbook 2005 & 2006 / American Osteopathic Association Yearbook 2005 & 2006.iso / mac / app / calendar.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-07-22  |  12.2 KB  |  272 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. '''Calendar printing functions
  5.  
  6. Note when comparing these calendars to the ones printed by cal(1): By
  7. default, these calendars have Monday as the first day of the week, and
  8. Sunday as the last (the European convention). Use setfirstweekday() to
  9. set the first day of the week (0=Monday, 6=Sunday).'''
  10. import datetime
  11. __all__ = [
  12.     'error',
  13.     'setfirstweekday',
  14.     'firstweekday',
  15.     'isleap',
  16.     'leapdays',
  17.     'weekday',
  18.     'monthrange',
  19.     'monthcalendar',
  20.     'prmonth',
  21.     'month',
  22.     'prcal',
  23.     'calendar',
  24.     'timegm',
  25.     'month_name',
  26.     'month_abbr',
  27.     'day_name',
  28.     'day_abbr']
  29. error = ValueError
  30. January = 1
  31. February = 2
  32. mdays = [
  33.     0,
  34.     31,
  35.     28,
  36.     31,
  37.     30,
  38.     31,
  39.     30,
  40.     31,
  41.     31,
  42.     30,
  43.     31,
  44.     30,
  45.     31]
  46.  
  47. class _localized_month:
  48.     
  49.     def __init__(self, format):
  50.         self.format = format
  51.  
  52.     
  53.     def __getitem__(self, i):
  54.         data = [ datetime.date(2001, j, 1).strftime(self.format) for j in range(1, 13) ]
  55.         data.insert(0, '')
  56.         return data[i]
  57.  
  58.     
  59.     def __len__(self):
  60.         return 13
  61.  
  62.  
  63.  
  64. class _localized_day:
  65.     
  66.     def __init__(self, format):
  67.         self.format = format
  68.  
  69.     
  70.     def __getitem__(self, i):
  71.         data = [ datetime.date(2001, 1, j + 1).strftime(self.format) for j in range(7) ]
  72.         return data[i]
  73.  
  74.     
  75.     def __len__(self_):
  76.         return 7
  77.  
  78.  
  79. day_name = _localized_day('%A')
  80. day_abbr = _localized_day('%a')
  81. month_name = _localized_month('%B')
  82. month_abbr = _localized_month('%b')
  83. (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)
  84. _firstweekday = 0
  85.  
  86. def firstweekday():
  87.     return _firstweekday
  88.  
  89.  
  90. def setfirstweekday(weekday):
  91.     '''Set weekday (Monday=0, Sunday=6) to start each week.'''
  92.     global _firstweekday
  93.     if not None if weekday <= weekday else weekday <= SUNDAY:
  94.         raise ValueError, 'bad weekday number; must be 0 (Monday) to 6 (Sunday)'
  95.     
  96.     _firstweekday = weekday
  97.  
  98.  
  99. def isleap(year):
  100.     '''Return 1 for leap years, 0 for non-leap years.'''
  101.     if not year % 4 == 0 and year % 100 != 0:
  102.         pass
  103.     return year % 400 == 0
  104.  
  105.  
  106. def leapdays(y1, y2):
  107.     '''Return number of leap years in range [y1, y2).
  108.        Assume y1 <= y2.'''
  109.     y1 -= 1
  110.     y2 -= 1
  111.     return (y2 // 4 - y1 // 4 - y2 // 100 - y1 // 100) + (y2 // 400 - y1 // 400)
  112.  
  113.  
  114. def weekday(year, month, day):
  115.     '''Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12),
  116.        day (1-31).'''
  117.     return datetime.date(year, month, day).weekday()
  118.  
  119.  
  120. def monthrange(year, month):
  121.     '''Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for
  122.        year, month.'''
  123.     if not None if month <= month else month <= 12:
  124.         raise ValueError, 'bad month number'
  125.     
  126.     day1 = weekday(year, month, 1)
  127.     if month == February:
  128.         pass
  129.     ndays = mdays[month] + isleap(year)
  130.     return (day1, ndays)
  131.  
  132.  
  133. def monthcalendar(year, month):
  134.     """Return a matrix representing a month's calendar.
  135.        Each row represents a week; days outside this month are zero."""
  136.     (day1, ndays) = monthrange(year, month)
  137.     rows = []
  138.     r7 = range(7)
  139.     day = ((_firstweekday - day1) + 6) % 7 - 5
  140.     while day <= ndays:
  141.         row = [
  142.             0,
  143.             0,
  144.             0,
  145.             0,
  146.             0,
  147.             0,
  148.             0]
  149.         for i in r7:
  150.             if day <= day:
  151.                 pass
  152.             elif day <= ndays:
  153.                 row[i] = day
  154.             
  155.             day = day + 1
  156.         
  157.         rows.append(row)
  158.         continue
  159.         1
  160.     return rows
  161.  
  162.  
  163. def prweek(theweek, width):
  164.     '''Print a single week (no newline).'''
  165.     print week(theweek, width),
  166.  
  167.  
  168. def week(theweek, width):
  169.     '''Returns a single week in a string (no newline).'''
  170.     days = []
  171.     for day in theweek:
  172.         if day == 0:
  173.             s = ''
  174.         else:
  175.             s = '%2i' % day
  176.         days.append(s.center(width))
  177.     
  178.     return ' '.join(days)
  179.  
  180.  
  181. def weekheader(width):
  182.     '''Return a header for a week.'''
  183.     if width >= 9:
  184.         names = day_name
  185.     else:
  186.         names = day_abbr
  187.     days = []
  188.     for i in range(_firstweekday, _firstweekday + 7):
  189.         days.append(names[i % 7][:width].center(width))
  190.     
  191.     return ' '.join(days)
  192.  
  193.  
  194. def prmonth(theyear, themonth, w = 0, l = 0):
  195.     """Print a month's calendar."""
  196.     print month(theyear, themonth, w, l),
  197.  
  198.  
  199. def month(theyear, themonth, w = 0, l = 0):
  200.     """Return a month's calendar string (multi-line)."""
  201.     w = max(2, w)
  202.     l = max(1, l)
  203.     s = (month_name[themonth] + ' ' + `theyear`).center(7 * (w + 1) - 1).rstrip() + '\n' * l + weekheader(w).rstrip() + '\n' * l
  204.     for aweek in monthcalendar(theyear, themonth):
  205.         s = s + week(aweek, w).rstrip() + '\n' * l
  206.     
  207.     return s[:-l] + '\n'
  208.  
  209. _colwidth = 7 * 3 - 1
  210. _spacing = 6
  211.  
  212. def format3c(a, b, c, colwidth = _colwidth, spacing = _spacing):
  213.     '''Prints 3-column formatting for year calendars'''
  214.     print format3cstring(a, b, c, colwidth, spacing)
  215.  
  216.  
  217. def format3cstring(a, b, c, colwidth = _colwidth, spacing = _spacing):
  218.     '''Returns a string formatted from 3 strings, centered within 3 columns.'''
  219.     return a.center(colwidth) + ' ' * spacing + b.center(colwidth) + ' ' * spacing + c.center(colwidth)
  220.  
  221.  
  222. def prcal(year, w = 0, l = 0, c = _spacing):
  223.     """Print a year's calendar."""
  224.     print calendar(year, w, l, c),
  225.  
  226.  
  227. def calendar(year, w = 0, l = 0, c = _spacing):
  228.     """Returns a year's calendar as a multi-line string."""
  229.     w = max(2, w)
  230.     l = max(1, l)
  231.     c = max(2, c)
  232.     colwidth = (w + 1) * 7 - 1
  233.     s = `year`.center(colwidth * 3 + c * 2).rstrip() + '\n' * l
  234.     header = weekheader(w)
  235.     header = format3cstring(header, header, header, colwidth, c).rstrip()
  236.     for q in range(January, January + 12, 3):
  237.         s = s + '\n' * l + format3cstring(month_name[q], month_name[q + 1], month_name[q + 2], colwidth, c).rstrip() + '\n' * l + header + '\n' * l
  238.         data = []
  239.         height = 0
  240.         for amonth in range(q, q + 3):
  241.             cal = monthcalendar(year, amonth)
  242.             if len(cal) > height:
  243.                 height = len(cal)
  244.             
  245.             data.append(cal)
  246.         
  247.         for i in range(height):
  248.             weeks = []
  249.             for cal in data:
  250.                 if i >= len(cal):
  251.                     weeks.append('')
  252.                     continue
  253.                 weeks.append(week(cal[i], w))
  254.             
  255.             s = s + format3cstring(weeks[0], weeks[1], weeks[2], colwidth, c).rstrip() + '\n' * l
  256.         
  257.     
  258.     return s[:-l] + '\n'
  259.  
  260. EPOCH = 1970
  261. _EPOCH_ORD = datetime.date(EPOCH, 1, 1).toordinal()
  262.  
  263. def timegm(tuple):
  264.     '''Unrelated but handy function to calculate Unix timestamp from GMT.'''
  265.     (year, month, day, hour, minute, second) = tuple[:6]
  266.     days = (datetime.date(year, month, 1).toordinal() - _EPOCH_ORD) + day - 1
  267.     hours = days * 24 + hour
  268.     minutes = hours * 60 + minute
  269.     seconds = minutes * 60 + second
  270.     return seconds
  271.  
  272.